home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / DAEMON.C < prev    next >
C/C++ Source or Header  |  1991-10-29  |  6KB  |  184 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/users/jinx/microcode/RCS/daemon.c,v 9.29 1991/10/29 22:55:11 jinx Exp $
  4.  
  5. Copyright (c) 1987-1991 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* This file contains code for the Garbage Collection daemons.
  36.    There are currently two daemons, one for closing files which
  37.    have disappeared due to GC, the other for supporting object
  38.    hash tables where entries disappear when the corresponding
  39.    object is released due to GC.
  40.  
  41.    Both of these daemons should be written in Scheme, but since the
  42.    interpreter conses while executing Scheme programs, they are
  43.    unsafe.  The Scheme versions actually exist, but are commented out
  44.    of the appropriate runtime system sources. */
  45.  
  46. #include "scheme.h"
  47. #include "prims.h"
  48. #include "osio.h"
  49.  
  50. /* (CLOSE-LOST-OPEN-FILES file-list)
  51.    file-list is an assq-like list where the associations are weak
  52.    pairs rather than normal pairs.  This primitive destructively
  53.    removes those weak pairs whose cars are #F, and closes the
  54.    corresponding file descriptor contained in the cdrs. See io.scm in
  55.    the runtime system for a longer description. */
  56.  
  57. DEFINE_PRIMITIVE ("CLOSE-LOST-OPEN-FILES", Prim_close_lost_open_files, 1, 1, 0)
  58. {
  59.   PRIMITIVE_HEADER (1);
  60.   {
  61.     SCHEME_OBJECT file_list = (ARG_REF (1));
  62.     SCHEME_OBJECT * smash = (PAIR_CDR_LOC (file_list));
  63.     SCHEME_OBJECT cell = (*smash);
  64.     while (cell != EMPTY_LIST)
  65.       {
  66.     SCHEME_OBJECT weak_cell = (FAST_PAIR_CAR (cell));
  67.     if ((FAST_PAIR_CAR (weak_cell)) == SHARP_F)
  68.       {
  69.         OS_channel_close
  70.           (UNSIGNED_FIXNUM_TO_LONG (FAST_PAIR_CDR (weak_cell)));
  71.         cell = (FAST_PAIR_CDR (cell));
  72.         (*smash) = cell;
  73.       }
  74.     else
  75.       {
  76.         smash = (PAIR_CDR_LOC (cell));
  77.         cell = (*smash);
  78.       }
  79.       }
  80.   }
  81.   PRIMITIVE_RETURN (UNSPECIFIC);
  82. }
  83.  
  84. /* Utilities for the rehash daemon below */
  85.  
  86. /* This runs with GC locked, being part of a GC daemon.
  87.    It is also the case that the storage needed by this daemon is
  88.    available, since it was all reclaimed by the immediately preceeding
  89.    garbage collection, and at most that much is allocated now.
  90.    Therefore, there is no gc check here. */
  91.  
  92. static void
  93. DEFUN (rehash_pair, (pair, hash_table, table_size),
  94.        SCHEME_OBJECT pair AND SCHEME_OBJECT hash_table
  95.        AND long table_size)
  96. {
  97.   long object_datum, hash_address;
  98.   SCHEME_OBJECT * new_pair;
  99.  
  100.   object_datum = (OBJECT_DATUM (FAST_PAIR_CAR (pair)));
  101.   hash_address = (2 + (object_datum % table_size));
  102.   new_pair = Free;
  103.   *Free++ = (OBJECT_NEW_TYPE (TC_LIST, pair));
  104.   *Free++ = (FAST_MEMORY_REF (hash_table, hash_address));
  105.   FAST_MEMORY_SET (hash_table,
  106.            hash_address,
  107.            (MAKE_POINTER_OBJECT (TC_LIST, new_pair)));
  108.   return;
  109. }
  110.  
  111. static void
  112. DEFUN (rehash_bucket, (bucket, hash_table, table_size),
  113.        SCHEME_OBJECT * bucket AND SCHEME_OBJECT hash_table
  114.        AND long table_size)
  115. {
  116.   fast SCHEME_OBJECT weak_pair;
  117.  
  118.   while (*bucket != EMPTY_LIST)
  119.   {
  120.     weak_pair = (FAST_PAIR_CAR (*bucket));
  121.     if ((FAST_PAIR_CAR (weak_pair)) != SHARP_F)
  122.     {
  123.       rehash_pair (weak_pair, hash_table, table_size);
  124.     }
  125.     bucket = (PAIR_CDR_LOC (*bucket));
  126.   }
  127.   return;
  128. }
  129.  
  130. static void
  131. DEFUN (splice_and_rehash_bucket, (bucket, hash_table, table_size),
  132.        SCHEME_OBJECT * bucket AND SCHEME_OBJECT hash_table
  133.        AND long table_size)
  134. {
  135.   fast SCHEME_OBJECT weak_pair;
  136.  
  137.   while ((*bucket) != EMPTY_LIST)
  138.   {
  139.     weak_pair = (FAST_PAIR_CAR (*bucket));
  140.     if ((FAST_PAIR_CAR (weak_pair)) != SHARP_F)
  141.     {
  142.       rehash_pair (weak_pair, hash_table, table_size);
  143.       bucket = (PAIR_CDR_LOC (*bucket));
  144.     }
  145.     else
  146.       *bucket = (FAST_PAIR_CDR (*bucket));
  147.   }
  148.   return;
  149. }
  150.  
  151. /* (REHASH unhash-table hash-table)
  152.    Cleans up and recomputes hash-table from the valid information in
  153.    unhash-table after a garbage collection.
  154.    See hash.scm in the runtime system for a description. */
  155.  
  156. DEFINE_PRIMITIVE ("REHASH", Prim_rehash, 2, 2, 0)
  157. {
  158.   long table_size, counter;
  159.   SCHEME_OBJECT *bucket;
  160.   PRIMITIVE_HEADER (2);
  161.   table_size = (VECTOR_LENGTH (ARG_REF (1)));
  162.  
  163.   /* First cleanup the hash table */
  164.   counter = table_size;
  165.   bucket = (MEMORY_LOC ((ARG_REF (2)), 2));
  166.   while ((counter--) > 0)
  167.     (*bucket++) = EMPTY_LIST;
  168.  
  169.   /* Now rehash all the entries from the unhash table and maybe splice
  170.      the buckets. */
  171.   counter = table_size;
  172.   bucket = (MEMORY_LOC ((ARG_REF (1)), 1));
  173.   while ((counter--) > 0)
  174.     {
  175.       if ((FAST_PAIR_CAR (*bucket)) == SHARP_T)
  176.     splice_and_rehash_bucket
  177.       ((PAIR_CDR_LOC (*bucket)), (ARG_REF (2)), table_size);
  178.       else
  179.     rehash_bucket ((PAIR_CDR_LOC (*bucket)), (ARG_REF (2)), table_size);
  180.       bucket += 1;
  181.     }
  182.   PRIMITIVE_RETURN (UNSPECIFIC);
  183. }
  184.